/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.ioui
 */
package KASystem;

import java.awt.Dialog;
import java.awt.Dialog.ModalityType;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author kristjan
 */
public class GUI extends javax.swing.JFrame {
    private static String FILENAME = "data.ser";
    
    ConferenceHandler conferenceHandler;

    /**
     * Creates new form GUI
     */
    public GUI() {
        if(!load()){
            conferenceHandler = new ConferenceHandler();
            generateConferences();
        }
        
        initComponents();
    }
    
    public boolean save() {
        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        ConferenceHandler ch = conferenceHandler;
        try {
            fos = new FileOutputStream(FILENAME);
            out = new ObjectOutputStream(fos);
            out.writeObject(ch);
            out.close();
        } catch (Exception ex) {
            return false;
        }

        return true;
    }

    public boolean load() {
        conferenceHandler = null;
        FileInputStream fis = null;
        ObjectInputStream in = null;
        try {
            fis = new FileInputStream(FILENAME);
            in = new ObjectInputStream(fis);
            conferenceHandler = (ConferenceHandler) in.readObject();
            in.close();
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    private void generateConferences() {
        Conference c = new Conference("Lorem", "Ipsum", 42);

        conferenceHandler.addConference(c);

        c = new Conference("dolor", "Ipsum", 42);

        conferenceHandler.addConference(c);

        c = new Conference("Sit Amet", "Ipsum", 42);
        conferenceHandler.addConference(c);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        addConferenceButton = new javax.swing.JButton();
        removeConferenceButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        conferenceText = new javax.swing.JTextArea();
        editConferenceButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
        jScrollPane10 = new javax.swing.JScrollPane();
        conferenceHotelList = new javax.swing.JList();
        jLabel6d = new javax.swing.JLabel();
        newHotelButton = new javax.swing.JButton();
        insertHotelButton = new javax.swing.JButton();
        removeHotelButton = new javax.swing.JButton();
        jScrollPane11 = new javax.swing.JScrollPane();
        hotelList = new javax.swing.JList();
        HotelsLabel = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane7 = new javax.swing.JScrollPane();
        conferenceDaytripList = new javax.swing.JList();
        jScrollPane8 = new javax.swing.JScrollPane();
        daytripList = new javax.swing.JList();
        newDaytripButton = new javax.swing.JButton();
        insertDaytripButton = new javax.swing.JButton();
        removeDaytripButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane9 = new javax.swing.JScrollPane();
        participantList = new javax.swing.JList();
        removeParticipantButton = new javax.swing.JButton();
        addParticipantButton = new javax.swing.JButton();
        jScrollPane6 = new javax.swing.JScrollPane();
        participantTextArea = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        conferenceList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jPanel1ComponentShown(evt);
            }
        });

        addConferenceButton.setText("Add");
        addConferenceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addConferenceButtonActionPerformed(evt);
            }
        });

        removeConferenceButton.setText("Remove");
        removeConferenceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeConferenceButtonActionPerformed(evt);
            }
        });

        conferenceText.setColumns(20);
        conferenceText.setEditable(false);
        conferenceText.setLineWrap(true);
        conferenceText.setRows(5);
        conferenceText.setWrapStyleWord(true);
        conferenceText.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jScrollPane2.setViewportView(conferenceText);

        editConferenceButton.setText("Edit");
        editConferenceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editConferenceButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(removeConferenceButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editConferenceButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addConferenceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                .addGap(14, 14, 14))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(addConferenceButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeConferenceButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editConferenceButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Conferences", jPanel1);

        jScrollPane10.setViewportView(conferenceHotelList);

        jLabel6d.setText("Hotel for this conference");

        newHotelButton.setText("New");
        newHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newHotelButtonActionPerformed(evt);
            }
        });

        insertHotelButton.setText("<<");
        insertHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertHotelButtonActionPerformed(evt);
            }
        });

        removeHotelButton.setText(">>");
        removeHotelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeHotelButtonActionPerformed(evt);
            }
        });

        jScrollPane11.setViewportView(hotelList);

        HotelsLabel.setText("Hotels");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(303, Short.MAX_VALUE)
                .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(271, 271, 271))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(newHotelButton)
                                .addComponent(insertHotelButton, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(removeHotelButton, javax.swing.GroupLayout.Alignment.TRAILING)))
                        .addComponent(jLabel6d))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(HotelsLabel)
                            .addGap(0, 313, Short.MAX_VALUE))
                        .addComponent(jScrollPane11, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE))
                    .addContainerGap()))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(173, 173, 173)
                .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(261, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel6d)
                        .addComponent(HotelsLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(newHotelButton)
                            .addGap(72, 72, 72)
                            .addComponent(insertHotelButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(removeHotelButton)
                            .addGap(0, 245, Short.MAX_VALUE))
                        .addComponent(jScrollPane10)
                        .addComponent(jScrollPane11))
                    .addGap(11, 11, 11)))
        );

        jTabbedPane1.addTab("Hotels", jPanel2);

        jLabel4.setText("Daytrips");

        jLabel5.setText("Daytrips for this confernece");

        jScrollPane7.setViewportView(conferenceDaytripList);

        jScrollPane8.setViewportView(daytripList);

        newDaytripButton.setText("New");
        newDaytripButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDaytripButtonActionPerformed(evt);
            }
        });

        insertDaytripButton.setText("<<");
        insertDaytripButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertDaytripButtonActionPerformed(evt);
            }
        });

        removeDaytripButton.setText(">>");
        removeDaytripButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeDaytripButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(newDaytripButton)
                            .addComponent(insertDaytripButton, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(removeDaytripButton, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 288, Short.MAX_VALUE))
                    .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(newDaytripButton)
                        .addGap(72, 72, 72)
                        .addComponent(insertDaytripButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeDaytripButton)
                        .addGap(0, 245, Short.MAX_VALUE))
                    .addComponent(jScrollPane7)
                    .addComponent(jScrollPane8))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Daytrip", jPanel4);

        participantList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                participantListValueChanged(evt);
            }
        });
        jScrollPane9.setViewportView(participantList);

        removeParticipantButton.setText("Remove");
        removeParticipantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeParticipantButtonActionPerformed(evt);
            }
        });

        addParticipantButton.setText("Add");
        addParticipantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addParticipantButtonActionPerformed(evt);
            }
        });

        participantTextArea.setColumns(20);
        participantTextArea.setRows(5);
        jScrollPane6.setViewportView(participantTextArea);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addParticipantButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(removeParticipantButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane6)
                    .addComponent(jScrollPane9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(addParticipantButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeParticipantButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Participants", jPanel3);

        conferenceList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                conferenceListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(conferenceList);

        jLabel2.setText("Conference:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 91, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 579, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(7, 7, 7)
                .addComponent(jScrollPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel1ComponentShown
        updateConferences();
    }//GEN-LAST:event_jPanel1ComponentShown

    private void removeConferenceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeConferenceButtonActionPerformed
        int index = conferenceList.getSelectedIndex();

        if (index != -1) {
            conferenceHandler.deleteConference(index);
        }

        updateConferences();
    }//GEN-LAST:event_removeConferenceButtonActionPerformed

    private void conferenceListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_conferenceListValueChanged
        int index = conferenceList.getSelectedIndex();
        if (index != -1) {
            Conference con = conferenceHandler.getConferences().get(index);
            conferenceText.setText(con.getName() + " || " + con.getDescription() + " || " + con.getPrice() + "\n");

            ArrayList<Participant> participants = con.getParticipants();

            for (Participant p : participants) {
                conferenceText.append(p.toString() + " | " + p.getPrice() + "\n");
            }
        }

        updateParticipants();
        updateConferenceDaytripList();
        updateConferenceHotelList();

    }//GEN-LAST:event_conferenceListValueChanged

    private void addConferenceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addConferenceButtonActionPerformed
        //JOptionPane.showConfirmDialog(this, new AddConference(conferenceHandler));
        JDialog jd = new JDialog(this, "BLAH", true);
        jd.setContentPane(new AddConference(conferenceHandler));
        jd.pack();
        jd.setVisible(true);

        updateConferences();
    }//GEN-LAST:event_addConferenceButtonActionPerformed

    private void editConferenceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editConferenceButtonActionPerformed
        int index = conferenceList.getSelectedIndex();
        if (index != -1) {
            JDialog jd = new JDialog(this, "BLAH", true);
            jd.setContentPane(new AddConference(conferenceHandler, index));
            jd.pack();
            jd.setVisible(true);
        }
        updateConferences();
    }//GEN-LAST:event_editConferenceButtonActionPerformed

    private void addParticipantButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addParticipantButtonActionPerformed
        int index = conferenceList.getSelectedIndex();
        if (index != -1) {
            Conference con = conferenceHandler.getConferences().get(index);

            JDialog jd = new JDialog(this, "BLAH", ModalityType.APPLICATION_MODAL);
            jd.setContentPane(new AddParticipant(con));
            jd.pack();
            jd.setVisible(true);
        }
        updateParticipants();
    }//GEN-LAST:event_addParticipantButtonActionPerformed

    private void participantListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_participantListValueChanged
        int pIndex = participantList.getSelectedIndex();
        int cIndex = conferenceList.getSelectedIndex();
        Participant p = conferenceHandler.getConferences().get(cIndex).getParticipants().get(pIndex);

        participantTextArea.setText(p.toString());

    }//GEN-LAST:event_participantListValueChanged

    private void removeParticipantButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeParticipantButtonActionPerformed
        int index = conferenceList.getSelectedIndex();
        if (index != -1) {
            Conference con = conferenceHandler.getConferences().get(index);
            int participantIndex = participantList.getSelectedIndex();

            if (participantIndex != -1) {
                con.getParticipants().remove(participantIndex);
            }

        }
        updateParticipants();
    }//GEN-LAST:event_removeParticipantButtonActionPerformed

    private void newDaytripButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newDaytripButtonActionPerformed
        JDialog jd = new JDialog(this, "New Daytrip", true);
        jd.setContentPane(new newDayTrip(conferenceHandler));
        jd.pack();
        jd.setVisible(true);

        updateDaytripList();
    }//GEN-LAST:event_newDaytripButtonActionPerformed

    private void insertDaytripButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertDaytripButtonActionPerformed
        int index = daytripList.getSelectedIndex();
        int indexb = conferenceList.getSelectedIndex();

        if (index != -1) {
            Daytrip d = conferenceHandler.getDaytrips().get(index);

            if (indexb != -1) {

                conferenceHandler.getConferences().get(indexb).addDaytrip(d);
                updateConferenceDaytripList();
            }
        }

    }//GEN-LAST:event_insertDaytripButtonActionPerformed

    private void removeDaytripButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeDaytripButtonActionPerformed

        int index = conferenceDaytripList.getSelectedIndex();
        int indexb = conferenceList.getSelectedIndex();


        if (index != -1 && indexb != -1) {
            conferenceHandler.getConferences().get(indexb).removeConferences(index);
            updateConferenceDaytripList();
        }
    }//GEN-LAST:event_removeDaytripButtonActionPerformed

    private void newHotelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newHotelButtonActionPerformed
        JDialog jd = new JDialog(this, "New Hotel", true);
        jd.setContentPane(new AddHotel(conferenceHandler));
        jd.pack();
        jd.setVisible(true);

        updateHotelList();
    }//GEN-LAST:event_newHotelButtonActionPerformed

    private void insertHotelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertHotelButtonActionPerformed
        if (conferenceList.getSelectedIndex() != -1 && hotelList.getSelectedIndex() != -1) {
            Hotel h = conferenceHandler.getHotels().get(hotelList.getSelectedIndex());
            conferenceHandler.getConferences().get(conferenceList.getSelectedIndex()).addHotel(h);
        }
        updateConferenceHotelList();
    }//GEN-LAST:event_insertHotelButtonActionPerformed

    private void removeHotelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeHotelButtonActionPerformed
        if (conferenceList.getSelectedIndex() != -1 && conferenceHotelList.getSelectedIndex() != -1) {
            Hotel h = conferenceHandler.getConferences().get(conferenceList.getSelectedIndex()).getHotels().get(conferenceHotelList.getSelectedIndex());
            conferenceHandler.getConferences().get(conferenceList.getSelectedIndex()).getHotels().remove(h);
        }
        updateConferenceHotelList();
    }//GEN-LAST:event_removeHotelButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if(!save()){
            System.out.println("Saving failed!");
        }
    }//GEN-LAST:event_formWindowClosing

    private void updateConferences() {
        ArrayList<Conference> cAL = conferenceHandler.getConferences();
        String[] stringArray = new String[cAL.size()];
        int index = conferenceList.getSelectedIndex();

        for (int i = 0; i < stringArray.length; i++) {
            stringArray[i] = cAL.get(i).toString();
        }

        conferenceList.setListData(stringArray);
    }

    private void updateParticipants() {
        int index = conferenceList.getSelectedIndex();

        if (index == -1) {
            return;
        }

        ArrayList<Participant> cAL = conferenceHandler.getConferences().get(index).getParticipants();
        String[] stringArray = new String[cAL.size()];



        for (int i = 0; i < stringArray.length; i++) {
            stringArray[i] = cAL.get(i).getName();
        }

        participantList.setListData(stringArray);
    }

    private void updateConferenceDaytripList() {
        int index = conferenceList.getSelectedIndex();

        Conference c = conferenceHandler.getConferences().get(index);

        String[] stringArray = new String[c.getDaytrips().size()];

        for (int i = 0; i < stringArray.length; i++) {
            stringArray[i] = c.getDaytrips().get(i).getName();
        }
        conferenceDaytripList.setListData(stringArray);
    }

    private void updateDaytripList() {

        String[] stringArray = new String[conferenceHandler.getDaytrips().size()];

        for (int i = 0; i < stringArray.length; i++) {
            stringArray[i] = conferenceHandler.getDaytrips().toString();
        }
        daytripList.setListData(stringArray);
    }

    private void updateHotelList() {
        String[] stringArray = new String[conferenceHandler.getHotels().size()];

        for (int i = 0; i < stringArray.length; i++) {
            stringArray[i] = conferenceHandler.getHotels().get(i).toString();
        }
        hotelList.setListData(stringArray);
    }

    private void updateConferenceHotelList() {
        String[] stringArray = new String[conferenceHandler.getConferences().get(conferenceList.getSelectedIndex()).getHotels().size()];

        for (int i = 0; i < stringArray.length; i++) {
            stringArray[i] = conferenceHandler.getConferences().get(conferenceList.getSelectedIndex()).getHotels().get(i).toString();
        }

        conferenceHotelList.setListData(stringArray);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel HotelsLabel;
    private javax.swing.JButton addConferenceButton;
    private javax.swing.JButton addParticipantButton;
    private javax.swing.JList conferenceDaytripList;
    private javax.swing.JList conferenceHotelList;
    private javax.swing.JList conferenceList;
    private javax.swing.JTextArea conferenceText;
    private javax.swing.JList daytripList;
    private javax.swing.JButton editConferenceButton;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JList hotelList;
    private javax.swing.JButton insertDaytripButton;
    private javax.swing.JButton insertHotelButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6d;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JScrollPane jScrollPane11;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton newDaytripButton;
    private javax.swing.JButton newHotelButton;
    private javax.swing.JList participantList;
    private javax.swing.JTextArea participantTextArea;
    private javax.swing.JButton removeConferenceButton;
    private javax.swing.JButton removeDaytripButton;
    private javax.swing.JButton removeHotelButton;
    private javax.swing.JButton removeParticipantButton;
    // End of variables declaration//GEN-END:variables
}
